package de.koller.worlders.engine.tiles;

import de.koller.oceanFramework.math.Vec;
import de.koller.oceanFramework.util.ColorIntUtil;
import de.koller.worlders.WorldersRoot;
import de.koller.worlders.engine.entity.IRenderable;
import de.koller.worlders.engine.entity.deco.Deco;
import de.koller.worlders.engine.entity.fog.Fog;
import de.koller.worlders.engine.entity.plattform.Plattform;
import de.koller.worlders.engine.tiles.data.MapData;
import de.koller.worlders.engine.tiles.light.LightSource;
import de.koller.worlders.engine.util.WLightWeightNode;
import de.koller.worlders.render.VisualMap;

public class Tile extends WLightWeightNode implements ITile {
	
	public static final Vec size = new Vec( 1, 1 );
	private final int x, y;
	
	private Deco deco;
	private Fog fog;
	
	private ITile tileEntity = null;
	
	private IRenderable visual = VisualMap.backTile;

	private LightSource lightsource;
	private int drawLight;

	public MapData assignedMapData;
	
	public Tile( int x, int y ) {
		this( x, y, null );
	}
	
	public Tile( int x, int y, ITile tileEntity ) {
		this.tileEntity = tileEntity;
		
		this.x = x;
		this.y = y;
	}

	@Override
	public IRenderable getVisual() {
		return visual;
	}

	public void addDeco( Deco deco ) {
		if( this.deco != null )
			throw new UnsupportedOperationException( "release of deco not yet implemented" );
		this.deco = deco;
		this.addNode( deco, this );
	}
	
	public void addFog( Fog fog ) {
		if( this.fog != null )
			throw new UnsupportedOperationException( "release of fog not yet implemented" );
		this.fog = fog;
		this.addNode( fog, this );
	}

	@Override
	public void generate( WorldersRoot root, Object... references ) {
		super.generate( root, references );
		
		if( tileEntity != null ) {
			this.addNode( tileEntity, this );
		}
		
		if( root == null )
			return;
		
		if( this instanceof MOTile == false ) {
			int lc = root.getTileMap().tileHolder.getLightData( 
					this.getPosition().getXi(), this.getPosition().getYi() );
			if( ColorIntUtil.getRValue( lc ) != 0 || ColorIntUtil.getGValue( lc ) != 0 || ColorIntUtil.getBValue( lc ) != 0 )
				lightsource = root.getTileMap().getLight().addLightSource(
						this.getPosition().getXi(), this.getPosition().getYi(), lc );	
			
			int dc = root.getTileMap().tileHolder.getDrawData( 
					this.getPosition().getXi(), this.getPosition().getYi() );
			drawLight= dc;
		}
		
	}

	@Override
	public void ungenerate() {
		super.ungenerate();

		if( lightsource != null )
			root.getTileMap().getLight().removeLightSource( lightsource );

		if( assignedMapData != null )
			assignedMapData.tileReleased();
	}
	
	public ITile getTileEntity() {
		return tileEntity;
	}
	
	public void resetTileEntity() {

	}


	@Override
	public Vec getPosition() {
		return new Vec( x, y );
	}

	@Override
	public Vec getSize() {
		return size;
	}

	public Deco getDeco() {
		return deco;
	}

	public Fog getFog() {
		return fog;
	}

	public void setVisual( IRenderable visual ) {
		this.visual = visual;
	}
	
	public float getLightTranslucents() {
		if( tileEntity == null )
			return 1;
		if( this.getTileEntity() instanceof Plattform )
			return 10;
		else
			return 1;
	}

	@Override
	public float getZ() {
		return 0;
	}

	public int getDrawLight() {
		return drawLight;
	}
	
}
